/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.client.gui;

import java.awt.event.*;

import javax.swing.*;

import info.clearthought.layout.TableLayout;
import com.dg.gaming.client.ClientConnectionThread;
import com.dg.gaming.api.common.msg.MsgChatClient;
import com.dg.gaming.api.common.msg.MsgChatPrivate;
import com.dg.gaming.api.common.util.DGLabels;
import com.dg.gaming.api.common.AbstractConnectionThread;

/**
 * Little message component which can be used in several ways.  These include
 * boardcast message (all clients recieve messages), room messages (only users
 * at a room/table) will recieve it and private message between 2 people.
 *
 * @author  Bob Marks
 * @version Beta 0.3
 */
public class ChatGameComponent extends DGPanel {
    // Declare GUI elements

    /** Main message ouput box. */
    protected JTextArea messageOutput;

    /** Small message input box. */
    protected JTextField messageInput;

    private DGScrollPane scrolledMessageOutput; // Scroll pane
    private DGButton sendButton; // send button

    // Declare connection
    private AbstractConnectionThread conn;

    /** Sending username. */
    protected long usernameFrom = -1L;

    /** Receiving username. */
    protected long usernameTo = -1L;

    /**
     * Constructor for a message component.
     *
     * @param height   Hieght of the message component in rows.
     */
    public ChatGameComponent(AbstractConnectionThread conn, int height) {
        this.conn = conn;
        this.usernameFrom = conn.getUserId();

        // Create message output, input and send button components
        this.messageOutput = new JTextArea(height, height);
        this.messageOutput.setEditable(false);
        this.messageOutput.setLineWrap(true);
        this.messageOutput.setWrapStyleWord(true);
        this.scrolledMessageOutput = new DGScrollPane(this.messageOutput);

        this.messageInput = new JTextField();
        this.sendButton = new DGButton(DGLabels.getInstance().get("send"));

        // Create a panel for the button components
        double pref = TableLayout.PREFERRED, fill = TableLayout.FILL;
        double[][] sizes = { {fill}, {fill, pref}
        };
        setLayout(new TableLayout(sizes));
        sizes = new double[][] { {TableLayout.FILL, pref}, {pref}
        };
        JPanel bottomPanel = new JPanel(new TableLayout(sizes));

        // Add components to this panel
        bottomPanel.add(this.messageInput, "0,0");
        bottomPanel.add(this.sendButton, "1,0");
        add(this.scrolledMessageOutput, "0,0");
        add(bottomPanel, "0,1");

        // Add title to the component
        setBorder(
                BorderFactory.createTitledBorder(
                        BorderFactory.createEtchedBorder(),
                        DGLabels.getInstance().get("chat")
                )
                );

        // Add listener to the send event
        this.sendButton.addActionListener(
                new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        }
        );

        this.messageInput.addKeyListener(
                new KeyAdapter() {
            // respond to the enter key
            public void keyPressed(KeyEvent keyevent) {
                if (keyevent.getKeyCode() == 10) {
                    sendMessage();
                }
            }
        }
        );
    }

    /**
     * Sets the enabled status of component and message input box.
     *
     * @see java.awt.Component#setEnabled(boolean)
     */
    public void setEnabled(boolean enabled) {
        this.sendButton.setEnabled(enabled);
        this.messageInput.setEditable(enabled);
    }

    /**
     * Set the username to of this component (not required if the component
     * is in broadcast mode).
     *
     * @param usernameTo   Username to send message to.
     */
    public void setUsernameTo(long usernameTo) {
        this.usernameTo = usernameTo;
    }

    /**
     * Send a message.  Message can be a private message or a braodcast message.
     */
    protected void sendMessage() {
        String messageText = this.messageInput.getText();

        boolean a = !messageText.equals("");
        if (this.conn != null && !messageText.equals("")) {
            this.messageOutput.append(this.usernameFrom + ": " + messageText +
                                      "\n");

            // Send to a user
            MsgChatClient chatMessage = null;
            if (this.usernameTo != -1L) {
                chatMessage = new MsgChatPrivate(messageText, this.usernameTo);
            }
            // Else broad cast to everyone!
            else {
                chatMessage = new MsgChatClient(messageText);
            }

            // Send message and blank the message input box.
            this.conn.send(chatMessage);
            this.messageInput.setText("");
        }
    }

    /**
     * Receives a message from the server.
     *
     * @param messageFrom   Who the message is from.
     * @param message       Text of the message.
     */
    public void receiveMessage(String messageFrom, String message) {
        this.messageOutput.append(messageFrom + ": " + message + "\n");
        this.messageOutput.setCaretPosition(this.messageOutput.getText().length());
    }
}
